﻿using HookupAppForm.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace HookupAppForm
{
    public class APIClient
    {
        //默认远程API地址
        public string RemoteHost { get; set; } = "http://localhost:21021";

        private MultipartFormDataContent content { get; set; }

        public string TenantId { get; set; }
        public string Token { get; set; }
        public string Accecpt { get; set; } = "application/json";
        /// <summary>
        /// 实例化API客户端,使用自定义远程API地址
        /// </summary>
        /// <param name="remoteHost">API链接，实例化APIClient时传入链接</param>
        public APIClient(string remoteHost) : base()
        {
            this.RemoteHost = remoteHost;
            content = new MultipartFormDataContent();
        }

        /// <summary>
        /// 添加固定参数
        /// </summary>
        /// <param name="key">参数键</param>
        /// <param name="value">参数值</param>
        public void AddParameters(string key, object value)
        {
            content.Add(new StringContent(value.ToString()), key);
        }

        /// <summary>
        /// 添加要上传的文件
        /// </summary>
        /// <param name="key">参数键</param>
        /// <param name="path">客户端文件地址</param>
        /// <param name="filename">文件名称</param>
        public void AddFiles(string key, string path, string filename)
        {
            content.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(path)), key, filename);
        }

        /// <summary>
        /// 提交请求
        /// </summary>
        /// <returns></returns>
        public string Post(string url, string param)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    StringContent content = new StringContent(param, Encoding.UTF8, "application/json");
                    if (!string.IsNullOrEmpty(TenantId))
                    {
                        client.DefaultRequestHeaders.Add("Abp.TenantId", TenantId);
                        client.DefaultRequestHeaders.Add("Abp-TenantId", TenantId);
                    }
                    if (!string.IsNullOrEmpty(Token))
                        client.DefaultRequestHeaders.Add("Authorization", Token);
                    client.DefaultRequestHeaders.Add("Accept", Accecpt);
                    client.DefaultRequestHeaders.Add("Connection", "keep-alive");
                    return client.PostAsync(RemoteHost + url, content).Result.Content.ReadAsStringAsync().Result;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                content = new MultipartFormDataContent();
            }
        }
        public string Get(string url)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    if (!string.IsNullOrEmpty(TenantId))
                    {
                        client.DefaultRequestHeaders.Add("Abp.TenantId", TenantId);
                        client.DefaultRequestHeaders.Add("Abp-TenantId", TenantId);
                    }
                    client.DefaultRequestHeaders.Add("Accept", Accecpt);
                    if (!string.IsNullOrEmpty(Token))
                        client.DefaultRequestHeaders.Add("Authorization", Token);
                    client.DefaultRequestHeaders.Add("Connection", "keep-alive");
                    return client.GetAsync(RemoteHost + url).Result.Content.ReadAsStringAsync().Result;

                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                content = new MultipartFormDataContent();
            }
        }
        public string Put(string url, string param)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    StringContent content = new StringContent(param, Encoding.UTF8, "application/json");
                    if (!string.IsNullOrEmpty(TenantId))
                    {
                        client.DefaultRequestHeaders.Add("Abp.TenantId", TenantId);
                        client.DefaultRequestHeaders.Add("Abp-TenantId", TenantId);
                    }
                    if (!string.IsNullOrEmpty(Token))
                        client.DefaultRequestHeaders.Add("Authorization", Token);
                    client.DefaultRequestHeaders.Add("Accept", Accecpt);
                    client.DefaultRequestHeaders.Add("Connection", "keep-alive");
                    return client.PutAsync(RemoteHost + url, content).Result.Content.ReadAsStringAsync().Result;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                content = new MultipartFormDataContent();
            }
        }

        public string Delete(string url)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    if (!string.IsNullOrEmpty(TenantId))
                    {
                        client.DefaultRequestHeaders.Add("Abp.TenantId", TenantId);
                        client.DefaultRequestHeaders.Add("Abp-TenantId", TenantId);
                    }
                    client.DefaultRequestHeaders.Add("Accept", Accecpt);
                    if (!string.IsNullOrEmpty(Token))
                        client.DefaultRequestHeaders.Add("Authorization", Token);
                    client.DefaultRequestHeaders.Add("Connection", "keep-alive");
                    return client.DeleteAsync(RemoteHost + url).Result.Content.ReadAsStringAsync().Result;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                content = new MultipartFormDataContent();
            }
        }
    }
}
